#!/usr/bin/python3
# -*- coding:utf-8 -*-

import os
import re

from job.src.function.basic_opration import get_TargetTime
from job.src.function.http_opration import DingRebot
from job.src.function.queryJira import get_jsession, query_issue, queryModiyMainProcess, data_parse
from job.src.function.record_log import mylog


class Jobs:
    def __init__(self):
        pass


    def query_jira_info(self, dd_info, jira_login, jsql, names_info):
        jsesion = get_jsession(**jira_login)
        jrdd = DingRebot(url=dd_info['dd_url'])
        for name, phone in names_info.items():
            jira_sql = jsql.format(name)
            res_list = query_issue(jira_sql, jsesion)
            if len(res_list) > 0:
                info = ','.join(res_list)
                tips = f'JIRA通知:{info}；{dd_info["dd_tip"]}'
                tester = [phone]
                jrdd.post(tips, name_list=tester)
            else:
                mylog.info(f'{name} 没有待验证的缺陷。')

    def query_ts_modification(self, dd_info, login_info, form_data, names_info):
        params = form_data.get('params', {})
        form_data.pop('params')
        before_day = params.get('prewarn_days', 4)
        cur_date = get_TargetTime(before_day, "%Y-%m-%d")
        enddate = {'promiseDate2': cur_date}
        params.update(enddate)
        type_tip = dd_info.get('type_tip', 1)
        reslst = queryModiyMainProcess(login_info, form_data, params)
        resdic = data_parse(reslst, type_tip)
        dd = DingRebot(url=dd_info['dd_url'])
        if type_tip == 0:
            for key, value in resdic.items():
                tester = []
                for k, v in names_info.items():
                    if re.search(key, k):
                        tester = [v]
                if not tester:
                    tips = f'TS通知:{value.rstrip(",")}{dd_info["dd_tip"]}。尚未分配测试，请各责任人主动认领验证！'
                    dd.post(tips, isAtAll=True)
                else:
                    tips = f'TS通知:{value.rstrip(",")}{dd_info["dd_tip"]}'
                    dd.post(tips, name_list=tester)
        elif type_tip == 1:
            for version, names in resdic.items():
                sendFlag = False
                verList = dd_info.get('ver_type')
                if version:
                    for verFlag in verList:
                        mylog.info(f'verFlag: {verFlag} ; {version}')
                        checkFlag = re.search(verFlag, version, re.I)
                        if checkFlag:
                            sendFlag= True
                            break
                # tester = []
                # if names:
                #     for name in names:
                #         for k, v in names_info.items():
                #             if re.search(name, k):
                #                 tester.append(v)
                if sendFlag:
                    tips = f'TS通知:{version}{dd_info["dd_tip"]}'
                    dd.post(tips, isAtAll=True)
                # if not tester:
                #     tips = f'TS通知:{version}{dd_info["dd_tip"]}'
                #     dd.post(tips, isAtAll=True)
                # else:
                #     tips = f'TS通知:{version}{dd_info["dd_tip"]}'
                #     dd.post(tips, name_list=tester)
        return True

